<template>
  <div class="countersign-product">
    <el-dialog v-loading="loading" element-loading-background="rgba(0, 0, 0, 0.2)" :visible.sync="dialogShow" title="确认产品清单" :close-on-click-modal="false" @close="handleBack">
      <el-form ref="form" :model="formData" :rules="formRules" label-width="80px">
        <el-form-item prop="confirmResult" label="确认结果" :rules="{ required: true, message: '请输入意见说明' }">
          <c-dict-select v-model="formData.confirmResult" dict-type-code="REVIEW_RESULT" placeholder="确认结果" />
          <!-- <el-select v-model="formData.confirmResult" placeholder="确认结果" clearable>
            <el-option label="同意" value="1" />
            <el-option label="不同意" value="0" />
          </el-select> -->
        </el-form-item>
        <el-form-item prop="confirmAdvice" label="意见说明" :rules="[{ required: formData.confirmResult === '0', message: '请输入意见说明' }]">
          <el-input v-model="formData.confirmAdvice" type="textarea" :autosize="{minRows:3}" placeholder="意见说明" clearable />
        </el-form-item>
        <!-- <el-form-item>
          <el-button type="primary" @click="confirmHandle">确定</el-button>
          <el-button type="primary" @click="clickHandle">取消</el-button>
        </el-form-item> -->
      </el-form>
      <div slot="footer">
        <el-button size="mini" @click="handleBack">取消</el-button>
        <el-button type="primary" size="mini" @click="confirmHandle">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { changeConfigState, getProConfigEditData } from '@/api/proConfig'
export default {
  name: 'CountersignProduct',
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    detailData: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      formData: {
        reason: '',
        result: ''
      },
      formRules: {
        confirmResult: [{ required: true, message: '请选择确认结果' }]
      }
    }
  },
  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    this.formData.id = this.detailData.id
    getProConfigEditData(this.formData.id).then(res => {
      if (res.success && res.data) {
        this.formData = res.data
        this.$set(this.formData, 'confirmResult', '')
        this.$set(this.formData, 'confirmAdvice', '')
        this.$nextTick(() => {
          this.$refs.form.clearValidate()
        })
      }
    })
  },
  methods: {
    // 返回
    handleBack() {
      this.dialogShow = false
      this.clearForm()
      this.$emit('success')
    },
    clearForm(done) {
      this.formData = {
        constData: [],
        staff: []
      }
      this.$refs.rulesForm && this.$refs.rulesForm.clearValidate()
      done && done()
    },
    confirmHandle() {
      this.$refs.form.validate(validate => {
        if (validate) {
          this.loading = true
          // return
          changeConfigState(this.formData).then(res => {
            if (res.success) {
              this.$message.success('操作成功')
              this.dialogShow = false
              this.clearForm()
              this.$emit('success', false)
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.countersign-product{
  display: inline-block;
  margin: 0 10px;
}
</style>
